/*
 * CAppStateCreateGame.cpp
 *
 *  Created on: 24/12/2012
 *      Author: Dalton
 */

#include "CAppStatePlayGame.h"
#include "CAppStateManager.h"
#include "CSurface.h"
#include <iostream>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include "SurfaceWrapper.h"
#include <string.h>
#include "util.h"
using namespace std;



CAppStatePlayGame CAppStatePlayGame::instance;
extern bool connected;

CAppStatePlayGame::CAppStatePlayGame(){
	conf = NICK;
	surface = NULL;
	font = NULL;
	bar = NULL;
	nickWrapper = "";
	ipWrapper = "";
	oldNick ="";
	userIp = NULL;
	color = {80,255,38};
	errorSurface = NULL;
	error = false;
	userNick = NULL;

	remoteip = NULL;
	tcpClient = NULL;



}

void CAppStatePlayGame::onKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
	if (conf == NICK) {
		if (sym == SDLK_BACKSPACE) {
			if (!nickWrapper.empty())
				nickWrapper.erase(nickWrapper.length() - 1);
		} else if (sym == SDLK_RETURN) {
			if (!nickWrapper.empty()) {
				// Do stuff with the text
			}
		} else {
			if (nickWrapper.length() < MAX_NICK_LENGHT) {
				if (unicode == ' ' || unicode >= 'a' && unicode <= 'z'
						|| unicode >= 'A' && unicode <= 'Z'
						|| unicode >= '0' && unicode <= '9')
					nickWrapper += (char) unicode;
			}
		}
	} else if (conf == IP) {
		if (sym == SDLK_BACKSPACE) {
			if (!ipWrapper.empty())
				ipWrapper.erase(ipWrapper.length() - 1);
		} else if (sym == SDLK_RETURN) {
			if (!ipWrapper.empty()) {
				// Do stuff with the text
			}
		} else {
			if (ipWrapper.length() < MAX_IP_LENGHT) {
				if (unicode == '.' || unicode >= '0' && unicode <= '9') {
					ipWrapper += (char) unicode;
				}
			}
		}

	}

}

void CAppStatePlayGame::onActivate(){
	if((surface = CSurface::onLoad(".//data/imgs/jogar-game.png")) == NULL){
		return;
	}

	if((bar = CSurface::onLoad(".//data/imgs/barra_text.png")) == NULL){
			return;
	}
	font = TTF_OpenFont(".//data/fonts/tahoma.TTF", 18);

	tcpClient = new CClientSocket();
	error = TTF_RenderText_Blended(font, "Nao foi possivel conectar ao servidor! tente novamente. ", color);
	SDL_EnableUNICODE(SDL_ENABLE);

}

void CAppStatePlayGame::onLButtonDown(int mX, int mY){

	if(mX >= 549 && mX <= 925 && mY >= 409 && mY <= 445 ){
		conf = NICK;
	} else if (mX >= 547 && mX <= 926 && mY >= 480 && mY <= 518 ){
		error = false;
		conf = IP;
	} else if (mX >= 555 && mX <= 694 && mY >= 555 && mY <= 590) {
		remoteip = new CIpAddress("192.168.208.128", 7777);
		//remoteip = new CIpAddress(ipWrapper.c_str(), 7777);
		if (!connected) {
			if (tcpClient->Connect(*remoteip)) {
				if (tcpClient->Ok()) {
					//textReceived = TTF_RenderText_Blended(font, "Connectado!", {80, 255, 38 });
					connected = true;
//					GAME_MSG msgTo;
//					msgTo.action = SEND_MSG;
//					strcpy(msgTo.msg,"To chegando e funfando!\0");
//					msg.LoadBytes(&msgTo);
//					msg.finish();
//					tcpClient->Send(msg);
					CAppStateManager::setActiveAppState(APPSTATE_STAGE_CLIENT);
				} else {
					error = true;
				}
			}
		} else {
			error = true;
		}
	} else if (mX >= 779 && mX <= 907 && mY >= 563 && mY <= 593 ){
		//entrou no cancelar
		CAppStateManager::setActiveAppState(APPSTATE_MENU);

	}
}


void CAppStatePlayGame::onDeactivate(){
	SDL_FreeSurface(surface);
	surface = NULL;
	SDL_FreeSurface(bar);
	bar = NULL;
	SDL_FreeSurface(userIp);
	userIp = NULL;
	SDL_FreeSurface(userNick);
	userNick = NULL;
	SDL_EnableUNICODE(SDL_DISABLE);
}

void CAppStatePlayGame::onLoop(){
	SDL_FreeSurface(userNick);
	userNick = NULL;
	userNick = TTF_RenderText_Blended(font, nickWrapper.c_str(), color);

	SDL_FreeSurface(userIp);
	userIp = NULL;
	userIp = TTF_RenderText_Blended(font, ipWrapper.c_str(), color);

}

void CAppStatePlayGame::onRender(SDL_Surface* surfDisplay){
	int nickAddition = 0;
	int ipAddition = 0;

	if(surface){
		CSurface::onDraw(surfDisplay, surface,0,0);
	}

	if(userNick != NULL){
		CSurface::onDraw(surfDisplay,userNick,555,417);
		nickAddition = userNick->w;

	}
	if(userIp != NULL){
		CSurface::onDraw(surfDisplay,userIp,555,490);
		ipAddition = userIp->w;
	}

	if(conf == NICK){
		CSurface::onDraw(surfDisplay,bar,555 + nickAddition, 417);
	}
	if(conf == IP){
		CSurface::onDraw(surfDisplay,bar,555 + ipAddition, 490);
	}

	if(error){
		CSurface::onDraw(surfDisplay,errorSurface ,161, 49);
	}

}

CAppStatePlayGame* CAppStatePlayGame::getInstance(){
	return &instance;
}

CClientSocket* CAppStatePlayGame::getTcpClient(){
	return tcpClient;
}


